package com.yjhh.tilemap;

/**
 * @author Michael Lawrence Evans :: michael@longliveman.com
 */

import java.io.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;

public class MapTilesPieces {

    public static void main(String[] args) throws IOException {
        MapTilesPieces tilePiecer = new MapTilesPieces();
        tilePiecer.run(args);
    }

    public void run(String[] args) throws IOException {

        File inFile = new File("E:\\map\\13map.jpg");

        BufferedImage image = ImageIO.read(inFile);

        //Create 256 by 256 tiles

        int nCols = image.getWidth() / 256;
        int nRows = image.getHeight() / 256;

        BufferedImage outputImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB_PRE);
        Graphics2D g = outputImage.createGraphics();

        for (int i = 0; i < nRows; i++) {
            for (int j = 0; j < nCols; j++) {

                BufferedImage tempImage = image.getSubimage(j * 256, i * 256, 256, 256);
                g.drawImage(tempImage, 0, 0, 256, 256, null);

                String filePath = "E:\\map\\tilemap\\13\\" + (6673 + j) + '\\';
                File file = new File(filePath);
                if (!file.exists()){
                    file.mkdirs();
                }
                File outFile = new File(filePath + (3554 + i) + ".jpg");

                try {
                    ImageIO.write(outputImage, "png", outFile);
                } catch (IOException e) {
                    System.err.println(e);
                    System.exit(1);
                }
            }
        }

    }

}